package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.ProductDao;
import dao.Factoty.DaoFactory;
import domain.Product;
import service.Factory.ServiceFactory;

/**
 * Servlet implementation class AddProductServlet
 */
@WebServlet("/AddProductServlet")
public class AddProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Product product = new Product();
		
		product.setId(UUID.randomUUID().toString());
		product.setCategory(request.getParameter("category"));
		product.setDescription(request.getParameter("description"));
		product.setImgurl(request.getParameter("imgurl"));
		product.setName(request.getParameter("name"));
		product.setPrice(Double.parseDouble(request.getParameter("price")));
		product.setPnum(Integer.parseInt(request.getParameter("pnum")));

		ProductDao productDao = DaoFactory.getProductDaoInstance();
		
		try {
			ServiceFactory.getProductServiceInstance().addProduct(product);
			response.sendRedirect(request.getContextPath()+ "/FindProductsServlet");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
